<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="资产分类" prop="cate_id">
            <treeselect v-model="queryParams.cate_id" :options="cateOptions" style="width:180px"
             :normalizer="normalizer" placeholder="请选择资产分类" />
        </el-form-item>
      <el-form-item label="型号名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入型号名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="厂商" prop="factory">
        <el-input
          v-model="queryParams.factory"
          placeholder="请输入厂商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['assets:model:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['assets:model:remove']"
        >删除</el-button>
      </el-col>
    <el-col :span="1.5">
    <el-button
        type="info"
        plain
        icon="el-icon-upload2"
        size="mini"
        @click="handleImport"
        v-hasPermi="['asset:warehouse:import']"
    >导入</el-button>
    </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['assets:model:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="modelList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="资产分类" align="center" prop="cate_name" />
      <el-table-column label="型号名称" align="center" prop="name" />
      <el-table-column label="厂商" align="center" prop="factory" />
      <el-table-column label="使用寿命" align="center" prop="life_time" />
      <el-table-column label="采购价" align="center" prop="price" />
      <el-table-column label="参数" align="center" prop="params" />
      <el-table-column label="计量单位" align="center" prop="unit" />
      <el-table-column label="市场价" align="center" prop="price1" />
      <el-table-column label="图例" align="center" prop="imgs" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.imgs" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['assets:model:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['assets:model:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="pager.page"
      :limit.sync="pager.limit"
      @pagination="getList"
    />

    <!-- 添加或修改资产型号对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="资产分类" prop="cate_id">
            <treeselect v-model="form.cate_id" :options="cateOptions" :normalizer="normalizer" placeholder="请选择资产分类" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
        <el-form-item label="型号名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入型号名称" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="厂商" prop="factory">
          <el-input v-model="form.factory" placeholder="请输入厂商" />
        </el-form-item>
          </el-col>
          </el-row>
        <el-row>
          <el-col :span="12">
        <el-form-item label="市场价" prop="price1">
          <el-input v-model="form.price1" placeholder="请输入市场价" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="采购价" prop="price">
          <el-input v-model="form.price" placeholder="请输入采购价" />
        </el-form-item>
          </el-col>
          </el-row>
        <el-row>
          <el-col :span="12">
        <el-form-item label="使用寿命" prop="life_time">
          <el-input v-model="form.life_time" placeholder="请输入使用寿命/月" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="计量单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入计量单位" />
        </el-form-item>
          </el-col>
          </el-row>
        <el-form-item label="参数" prop="params">
          <el-input v-model="form.params" type="textarea" rows="3" placeholder="请输入参数" />
        </el-form-item>
        <el-form-item label="图例" prop="imgs">
          <image-upload v-model="form.imgs"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 导入 -->
    <import-dialog :open="upload" model="asset_model" @cancel="importCancel" @success="importSuccess" />
  </div>
</template>

<script>
import { apiGet, apiPost, apiPut, apiDelete, apiDs, apiExport, apiDownload } from "@/api/openapi";
import ImportDialog from "@/components/ImportDialog";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {subTreeIds} from "../tree"

export default {
  name: "Model",
  components: {    Treeselect, ImportDialog  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资产型号表格数据
      modelList: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        cate_id: null,
        name: null,
        factory: null,
        unit: null,
        status: null,
        "@alias": "t",
        "@exp": {
          name: "lk",
          factory: "lk",
          unit: "lk",
          cate_id: "in",
        }
      },
      pager: {
        page: 1,
        limit: 10,
        order: "id desc",
      },
      // 表单参数
      form: {},
      upload:false,
      cateOptions:[],
      // 表单校验
      rules: {
        cateId: [
          { required: true, message: "资产分类不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "型号名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    apiDs('asset_category_select').then(response=>{
        this.cateOptions = this.handleTree(response.data, "value", "pid");
    });
  },
  methods: {
    /** 查询资产型号列表 */
    getList() {
      this.loading = true;
      let params = {};
      params['asset_model'] = {...this.queryParams};
      if(this.queryParams.cate_id != null && this.queryParams.cate_id === 0){
        params['asset_model']['cate_id'] = null;
      }else if(this.queryParams.cate_id != null && this.queryParams.cate_id != 0){
        params['asset_model']['cate_id'] = subTreeIds(this.cateOptions, this.queryParams.cate_id);
      }
      params['asset_category'] = {
          "@alias": "c",
          "@join": "LEFT c.id = t.cate_id",
          "@column":"name cate_name"
      };
      params['@pager'] = this.pager;
      apiGet(params).then(response => {
        this.modelList = response.data.rows;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 转换资产分类数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.value,
        label: node.label,
        children: node.children
      };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        cateId: null,
        name: null,
        factory: null,
        param1: null,
        param2: null,
        param3: null,
        param4: null,
        param5: null,
        lifeTime: null,
        price: null,
        params: null,
        unit: null,
        price1: null,
        imgs: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pager.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加资产型号";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
        let data = {};
        data['asset_model'] = { id:id};
      apiGet(data).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改资产型号";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
            let data = {};
            data['asset_model'] = this.form;
            data['@tags'] = ['asset_model'];
          if (this.form.id != null) {
            apiPut(data).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            apiPost(data).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除选中的资产型号数据？').then(function() {
        let data = {};
        data['asset_model'] = { id:ids, "@exp":{id:"in"}};
        data['@tags'] = ['asset_model'];
        return apiDelete(data);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload = true;
    },
    importSuccess(msg) {   
        this.upload = false;   
        this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
        this.getList();
    },
    importCancel() {      
        this.upload = false;   
    },
    /** 导出按钮操作 */
    handleExport() {
        let params = {};
      params['asset_model'] = this.queryParams;
      params['@pager'] = {page:1, limit:1000};
      apiExport(params).then(response => {
        apiDownload(response.msg);
      });
    }
  }
};
</script>
